<?php

namespace App\Backend\Http\Controllers;

use App\Border;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class BorderController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index(Request $request)
    {
        $where[] = ['id', '>', 0];
        if ($request->status) {
            $where['status'] = $request->status;
        }
        $data = $request->all();
        if ($request->name || $request->mobile) {
            unset($where['status']);
            $where['user_id'] = User::where($data)->value('id');
        }
        if (!empty($request->date)) {
            $date = explode(' - ', $request->date);
            $data['borders'] = Border::where($where)->whereBetween('created_at', [$date[0], $date[1]])->orderBy('id', 'desc')->paginate(20);
        } else {
            $data['borders'] = Border::where($where)->orderBy('created_at', 'desc')->paginate(20);
        }
        return view('backend::border.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($paytype)
    {
        return view('backend::border.create', ['paytype' => $paytype]);
    }


    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'paytype' => 'required',
            'admin_id' => 'required',
            'mobile' => 'required|regex:/^1[34578][0-9]{9}$/|exists:users',
            'coins' => 'sometimes|required|onehundred',
            'vip' => 'sometimes|required|in:3,12',
            'note' => 'sometimes',
        ]);
        $userinfo = User::where('mobile', $data['mobile'])->first();
        $data['user_id'] = $userinfo->id;
        if ($data['paytype'] == 1) {
            if (isset($data['coins'])) {
                $user['coins'] = $userinfo->coins + $data['coins'];
            }
            if ($request->is_vip == 1) {
                $user['is_vip'] = 1;
                if($userinfo->vip == 1){
                    $user['vipduetime'] = Carbon::parse('+' . $data['vip'] . 'months')->toDateTimeString();
                }else{
                    $user['vipduetime'] = Carbon::parse($userinfo->vipduetime)->addmonth($data['vip']);
                }
            }else{
                $data['vip'] = 0;
            }
        }else{
            $user['coins'] = $userinfo->coins - $data['coins'];
            $data['vip'] = 0;
        }
        Border::create($data);
        $userinfo->update($user);
        return redirect('/backend/border');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('backend::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('backend::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request)
    {
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy()
    {
    }
}
